package controller

import (
	"bluebell/logic"
	"bluebell/pkg/utils"
	"fmt"
	"github.com/gin-gonic/gin"
	"net"
	"net/http"
	"strconv"
)

// ConditionsListHandler 获取房屋信息选项列表
func ConditionsListHandler(ctx *gin.Context) {
	cityIdStr := ctx.Param("id")
	cityId, err := strconv.Atoi(cityIdStr)
	if err != nil {
		ResponseError(ctx, http.StatusBadRequest, "参数错误")
		return
	}
	conditions := logic.GetConditionList(cityId)
	ResponseSuccess(ctx, http.StatusOK, "OK", conditions)
}

// UploadHandler 获取房屋信息选项列表
func UploadHandler(ctx *gin.Context) {
	file, err := ctx.FormFile("file")
	//form, err := ctx.MultipartForm()
	//files := form.File("file")
	if err != nil {
		ResponseError(ctx, http.StatusBadRequest, "参数错误")
		return
	}
	ret := utils.CheckImageExt(file)
	if !ret {
		ResponseError(ctx, http.StatusBadRequest, "文件类型不合法")
		return
	}
	addrs, _ := net.InterfaceAddrs()
	for _, addr := range addrs {
		ipAddr, ok := addr.(*net.IPNet)
		if !ok {
			continue
		}
		if ipAddr.IP.IsLoopback() {
			continue
		}
		if !ipAddr.IP.IsGlobalUnicast() {
			continue
		}
		fmt.Println(ipAddr.IP.String())
	}

	fmt.Println(addrs)
	ResponseSuccess(ctx, http.StatusOK, "OK", file)
}
